<?php #//AJAX PHP PENNY AUCTION v.1.1

#///////////////////////////////////////////////////////////
#//  COPYRIGHT 2009 Aaron Helmlinger ALL RIGHTS RESERVED  //
#///////////////////////////////////////////////////////////


include "./includes/config.inc.php";


#///////////////////////////////////////////////////////
#// Get Current Time
#///////////////////////////////////////////////////////

   $TIME = mktime(date("H")+$SETTINGS['timecorrection'],date("i"),date("s"),date("m"), date("d"),date("Y"));

$date_time_now = date("YmdHis",$TIME);
$NOW = $date_time_now;


#///////////////////////////////////////////////////////
#// Get Auto Bidder Settings
#///////////////////////////////////////////////////////

$query_settings = "select * from PHPAUCTIONXL_proxybid_settings where id='1'";
                             $query_settings2 =  mysql_query ($query_settings);
                                $activated = mysql_result($query_settings2,0,"activated");


 #/////////////////////////////////////////////////////////////////////
#// CALCULATE SECONDS LEFT UNTIL AUCTION ENDS FUNCTION
#/////////////////////////////////////////////////////////////////////

   function timeDiff2($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;

}



#///////////////////////////////////////////////////////
#// Activate Auto Bidder
#///////////////////////////////////////////////////////

if ($activated == 1){


#///////////////////////////////////////////////////////
#// GET ACTIVE AUCTIONS FROM DATABASE 
#///////////////////////////////////////////////////////

$query = "select * from PHPAUCTIONXL_auctions
         WHERE closed='0' AND
         suspended='0' AND ";
         $query .= "starts<=".$NOW."
         order by id LIMIT 0,30";
$result = mysql_query($query);
$num_auction = mysql_num_rows($result);

$i = 0;


 // echo $num_auction;

while($i < $num_auction) {

              
#///////////////////////////////////////////////////////
#// SET VARIABLE FOR ITEM TO BID ON 
#///////////////////////////////////////////////////////
      
      $title	= mysql_result($result,$i,"title");
	$id	= mysql_result($result,$i,"id");
	 $starts 	= mysql_result($result,$i,"starts");
  $ends 	= mysql_result($result,$i,"ends");
  $increment = mysql_result($result,$i,"increment");
  $high_bidder 	= mysql_result($result,$i,"current_high_bidder");
	$current_bid 	= mysql_result($result,$i,"current_bid");
   $closed 	= mysql_result($result,$i,"closed");
	  $unique1 	= mysql_result($result,$i,"unique1");
     $unique2 	= mysql_result($result,$i,"unique2");
       $reserve 	= mysql_result($result,$i,"reserve_price");
           $bid_credits_reqd1 = mysql_result($result,$i,"bid_credits_reqd");
             $item_extend_activated = mysql_result($result,$i,"ae_activation");
  $item_extend_increment = mysql_result($result,$i,"ae_increment");
 $auction_id_index = $id;

#/////////////////////////////////////////////////////////////////////
#// CALCULATE SECONDS LEFT UNTIL AUCTION ENDS
#/////////////////////////////////////////////////////////////////////

$time_ends_seconds = substr ($ends, 0, 4); // Year
$time_ends_seconds .= "-";
$time_ends_seconds .= substr ($ends, 4, 2); // Month
$time_ends_seconds .= "-";
$time_ends_seconds .= substr ($ends, 6, 2); // Day
$time_ends_seconds .= " ";
$time_ends_seconds .= substr ($ends, 8, 2); // Hour
$time_ends_seconds .= ":";
$time_ends_seconds .= substr ($ends, 10, 2); // Minute
$time_ends_seconds .= ":";
$time_ends_seconds .= substr ($ends, 12, 2); // Second

$time_now_seconds = date("Y-m-d H:i:s",$TIME);
 

$time_end_difference_in_seconds = timeDiff2 ($time_now_seconds,$time_ends_seconds);
$seconds_until_auction_ends = $time_end_difference_in_seconds;

#///////////////////////////////////////////////////////
#// Get Auto Bidder Data Greater Than Current Bid
#///////////////////////////////////////////////////////

 if ( $time_end_difference_in_seconds < 30 ) {

 $num_proxybids = 0;

  $query_proxybids = "select * from PHPAUCTIONXL_proxybid WHERE itemid='$id' AND bid > '$current_bid' AND proxybidqty > 0 ORDER BY bid ASC";
$result_proxybids = mysql_query($query_proxybids);


$num_proxybids = mysql_num_rows($result_proxybids);



 if ( $num_proxybids > 0 ) {         
        
    //    echo $num_proxybids;
     //     echo "<BR><BR>";
        
#///////////////////////////////////////////////////////
#// Select Lowest Maximum Bid Auto Bidder User
#///////////////////////////////////////////////////////

$ii = 0;



//$userid	= mysql_result($result_proxybids,$ii,"userid");
  //  $proxybid	= mysql_result($result_proxybids,$ii,"bid");
  //  $proxybidqty	= mysql_result($result_proxybids,$ii,"proxybidqty");
          
      
   $going_to_bid = 0;         
    
while( $ii < $num_proxybids ) {

if ( $going_to_bid < 1 )  {

    $userid	= mysql_result($result_proxybids,$ii,"userid");
    $proxybid	= mysql_result($result_proxybids,$ii,"bid");
    $proxybidqty	= mysql_result($result_proxybids,$ii,"proxybidqty");

      // echo $userid;
      // echo $proxybid;
      // echo $proxybidqty;   


#///////////////////////////////////////////////////////
#// Check If Current Bidder
#///////////////////////////////////////////////////////

  // if ( $proxybidqty > 0 ) {  

$query_user = mysql_fetch_array(mysql_query("SELECT nick, bids_remaining FROM PHPAUCTIONXL_users WHERE id='$userid'"));
   $username = $query_user['nick'];
   $bids_remaining = $query_user['bids_remaining'];
   
   /*echo "<BR>";
   echo $ii;
   echo "-----";
   echo $username;
   echo "-----";
   echo $bids_remaining;
   echo "-----";
   echo $high_bidder;
   echo "-----";
   echo $id;
   echo "<BR>";  */
  
   
   
  // exit;
   
   
      
   if ( !($username==$high_bidder) && $bids_remaining > $bid_credits_reqd1 ) {
   

   
  $going_to_bid = 1;
   
   // echo "-----------------------------------------------";
   
   
  //   }
   
   }   
   }
   
  $ii++; } // WHILE $GOING_TO_BID != 1
  
  }
  
#///////////////////////////////////////////////////////
#// Place Auto Bidder Bid
#///////////////////////////////////////////////////////


 if ( $going_to_bid == 1 ) {

 // echo $going_to_bid;
//  echo $accounttype;
  
$increment = $increment;

$current_bid = $current_bid;

$high_bid = $increment + $current_bid;

$proxybidqty--;

$bids_remaining2 = $bids_remaining - $bid_credits_reqd1;

//echo $high_bid;

$query1 = "INSERT INTO PHPAUCTIONXL_bids VALUES";

$query1 .= "(NULL";
$query1 .= ",";
$query1 .= $id;
$query1 .= ",";
$query1 .= $userid;
$query1 .= ",";
$query1 .= $high_bid;
$query1 .= ",";
$query1 .= $NOW;
$query1 .= ",";
$query1 .= "1";
$query1 .= ",";
$query1 .= "'";
$query1 .= $username;
$query1 .= "'";
$query1 .= ",";
$query1 .= "'";
$query1 .= proxy;
$query1 .= "'";
$query1 .= ")";
     
   // echo $query1;
    //echo "<BR>";       
mysql_query($query1);


   
 
$query2="UPDATE PHPAUCTIONXL_auctions SET current_high_bidder='$username', current_bid='$high_bid' WHERE id='$id'";
   //echo $query2; 
   // echo "<BR>";
mysql_query($query2);

$query25="UPDATE PHPAUCTIONXL_proxybid SET proxybidqty='$proxybidqty' WHERE userid='$userid' AND itemid='$id'";
   //echo $query25; 
   // echo "<BR>";
mysql_query($query25);
   
   
 $query24="UPDATE PHPAUCTIONXL_users SET bids_remaining='$bids_remaining2' WHERE id='$userid'";
   //echo $query24; 
   // echo "<BR>";
mysql_query($query24);  
   
 $auctionends = $ends;   
   
        $TIMExyz = mktime(date("H")+$SETTINGS['timecorrection'],date("i"),date("s"),date("m"), date("d"),date("Y"));

   
if ( $item_extend_activated == 0 ) {  
    /**
        * NOTE: AUCTION AUTOEXTENSION
        */
        $EXTSETTINGS = mysql_fetch_array(mysql_query("SELECT * FROM PHPAUCTIONXL_auctionextension"));
        if ($EXTSETTINGS['status'] == 'enabled') {
          $__END = mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2), substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4));
            
                    $TIME_EXTENDxyz = $EXTSETTINGS['timebefore'] + 10;
            if (($__END - $TIME) <= $TIME_EXTENDxyz) {
                $auctionends = date("YmdHis", mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2) + $EXTSETTINGS['extend'] + 4, substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4)));
           
            }

          if (($__END - $TIME) > -1 ) {
               
              $query = "UPDATE PHPAUCTIONXL_auctions set ends='$auctionends' WHERE id=$auction_id";
               mysql_query($query);       
            }
           
            
             if (($__END - $TIME) <= 0 ) {
            
                    $closed = "1";
                    
                          }
                          
                          
        }
            }

         if ( $item_extend_activated > 0 ) {  
    /**
        * NOTE: AUCTION AUTOEXTENSION
        */
           if ( 1 == 1 ) {
          $__END = mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2), substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4));
            
                       $TIME_EXTENDxyz = $EXTSETTINGS['timebefore'] + 10;
            if (($__END - $TIME) <= $TIME_EXTENDxyz ) {
                $auctionends = date("YmdHis", mktime(substr($auctionends, 8, 2), substr($auctionends, 10, 2), substr($auctionends, 12, 2) + $item_extend_increment + 4, substr($auctionends, 4, 2), substr($auctionends, 6, 2), substr($auctionends, 0, 4)));
           
            }

         if (($__END - $TIME) > -1 ) {
               
              $query = "UPDATE PHPAUCTIONXL_auctions set ends='$auctionends' WHERE id=$auction_id";
              mysql_query($query);        
            }
           
            
             if (($__END - $TIME) <= 0 ) {
            
                    $closed = "1";
                    
                          }
                          
                          
        }
            }

   
   
   

   $going_to_bid = 0;
     
} // End Bid
} // TIME UNTIL END IS LESS THAN 30 SECONDS
$i++; //echo "end of script";
} // FOREACH LIVE AUCTION
} // SCRIPT ACTIVATED



   ?>